package my.tutorial.javacore.swing;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFormattedTextField;
import javax.swing.JTextArea;

public class CelsiusConverterGUI {

	private JFrame frame;
	private JTextField celsiusTextField;
	private JLabel fahrenheitLabel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CelsiusConverterGUI window = new CelsiusConverterGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CelsiusConverterGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		celsiusTextField = new JTextField();
		celsiusTextField.setText("0");
		celsiusTextField.setBounds(10, 10, 76, 21);
		frame.getContentPane().add(celsiusTextField);
		celsiusTextField.setColumns(10);

		JLabel lblCelsius = new JLabel("Celsius");
		lblCelsius.setBounds(106, 13, 106, 15);
		frame.getContentPane().add(lblCelsius);

		JButton convertButton = new JButton("Convert");
		convertButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (celsiusTextField.getText().isEmpty()) {
					celsiusTextField.setText("0");
				}
				int tempFahr = (int) (Double.parseDouble(celsiusTextField
						.getText()) * 1.8 + 32);
				fahrenheitLabel.setText(tempFahr + " Fahrenheit");
			}
		});
		convertButton.setBounds(10, 47, 76, 23);
		frame.getContentPane().add(convertButton);
		
		fahrenheitLabel = new JLabel("Fahrenheit");
		fahrenheitLabel.setBounds(106, 51, 106, 15);
		frame.getContentPane().add(fahrenheitLabel);
	}
}
